<?php

$sql = "SELECT menu,titulo,url FROM Menu_mount WHERE Automatico='0' ORDER BY menu,titulo";
$result = mysql_query($sql, $dbctl);
if($result == 0)
{
     printf("<h1>(function-7)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($result) > 0)
     {
          while($myrow = mysql_fetch_row($result))
          {
               $titulo = '';
               $menux = '';
               if(strstr($myrow[0], "button|") != "")
               {
                    continue;
               }
               if(strstr($myrow[1], ',') != "")
               {
                    list($titulo, $sub) = explode(",", $myrow[1]);
               }
               else
               {
                    $titulo = $myrow[1];
               }
               if($titulo == '')
               {
                    $titulo = $myrow[0];
               }
               $titulo = remover_acentos($titulo);
               $menux = remover_acentos($myrow[0]);
               $var = explode("/", $myrow[2]);
               if(count($var) > 1)
               {
                    $var = explode(".php", $var[1]);
               }
               else
               {
                    $var = explode(".php", $myrow[2]);
               }
               $file = $var[0] . ".inc.php";
               $extras = "<?php\n";
               $extras .= "session_start();\n";
               $extras .= "if(!isset(\$_SESSION['USER']))\n";
               $extras .= "{\n";
               $extras .= "     Header('Location: login.php');\n";
               $extras .= "     exit();\n";
               $extras .= "}\n";
               $extras .= "require 'define.php';\n";
               $extras .= "if(!isset(\$DIR_" . $menux . "_" . $titulo . "_EDITAR) && !isset(\$DIR_" . $menux . "_" . $titulo . "_LISTAR))\n";
               $extras .= "{\n";
               $extras .= "     Header('Location: index.php');\n";
               $extras .= "     exit();\n";
               $extras .= "}\n";
               $extras .= "?>\n\n";

               $tmp = sprintf("%s/includes_dir", $diretorio);
               if(!is_dir($tmp))
               {
                    mkdir($tmp);
               }
               $FILE = sprintf("%s/includes_dir/$file", $diretorio);
               if(isset($_REQUEST['Dir']))
               {
                    $fPHP = fopen($FILE, "w");
                    if(!$fPHP)
                    {
                         echo "<h1>Falha na Criação do Arquivo '" . $FILE . "'</h1>";
                         exit;
                    }
                    fprintf($fPHP, "%s", $extras);
                    fclose($fPHP);
                    printf("<h3 align='center'>Arquivo '%s' Gerado com Sucesso</h3>\n", $FILE);
               }
               else
               {
                    if(file_exists($FILE))
                    {
                         unlink($FILE);
                         printf("<h3 align='center'>Arquivo '%s' REMOVIDO com Sucesso</h3>\n", $FILE);
                    }
               }
          }
     }
}
?>
